package net.cyl.controller;

import net.cyl.controller.request.DomainAddRequest;
import net.cyl.service.DomainService;
import net.cyl.util.JsonData;
import net.cyl.vo.DomainVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/domain/v1")
public class DomainController {
    @Autowired
    private DomainService domainService;
    /**
     * 获取所有域名信息
     * @return
     */
    @GetMapping("/listAllDomain")
    public JsonData listAllDomain(){
        List<DomainVO> domainVOList =  domainService.listAllDomain();
        return JsonData.buildSuccess(domainVOList);

    }

    @PostMapping("/createDomain")
    public JsonData createDomain(@RequestBody DomainAddRequest domainAddRequest){
        DomainVO domainVO = domainService.createDomain(domainAddRequest);
        return JsonData.buildSuccess(domainVO);
    }




//    @Autowired
//    private RedisTemplate<Object,Object> redisTemplate;
//    @GetMapping("test")
//    public JsonData test(@RequestParam("code") String code, @RequestParam("accountNo") String accountNo){
//        //key1是短链码，ARG[1]是accountNo,ARG[2]过期时间
//        String script = "if redis.call('exists',KEYS[1]) == 0 then " +
//                "redis.call('set',KEYS[1],ARGV[1]);" +
//                "redis.call('expire',KEYS[1],ARGV[2]);" +
//                "return 1;" +
//                "elseif redis.call('get',KEYS[1]) == ARGV[1] then " +
//                "return 2;" +
//                "else " +
//                "return 0;" +
//                "end";
//        Long result = (Long) redisTemplate.execute(new DefaultRedisScript<>(script, Long.class), List.of(code), accountNo, 100);
//        return JsonData.buildSuccess(result);
//    }
}
